package com.yuerlive.rainbow.module.game.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mrd.game.message.RainbowMessage;
import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.utils.FormatUtil;
import com.yuerlive.rainbow.utils.UserProxy;

import java.util.List;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xiaoyi on 2017/7/24.
 */

public class BankerListAdapter extends BaseAdapter {

    private List<RainbowMessage.User> mDatas;
    private Context context;

    public BankerListAdapter(List<RainbowMessage.User> mDatas, Context context) {
        this.mDatas = mDatas;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context)
                    .inflate(R.layout.layout_bankerlist_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        RainbowMessage.User user = mDatas.get(position);
        holder.tvSort.setText(position + 1 + "");
        setText(user.getNickname(), holder.tvName);
        holder.tvSweet.setText(FormatUtil.formatDealTotal(user.getSweetNum()));
        String id = UserProxy.getUser().getUserId();
        if (user.getUserId() == Integer.parseInt(id)) {
            holder.tvSweet.setTextColor(holder.colorRed);
            holder.tvSort.setTextColor(holder.colorRed);
            holder.tvName.setTextColor(holder.colorRed);
        } else {
            holder.tvSweet.setTextColor(holder.colorNormal);
            holder.tvSort.setTextColor(holder.colorNormal);
            holder.tvName.setTextColor(holder.colorNormal);
        }
        return convertView;
    }

    private void setText(String text, TextView textView) {
        if (text.length() > 6) {
            text = text.substring(0, 6) + "...";
            textView.setText(text);
        } else {
            textView.setText(text);
        }
    }

    static class ViewHolder {
        @BindView(R.id.tvSort)
        TextView tvSort;
        @BindView(R.id.tvName)
        TextView tvName;
        @BindView(R.id.tvSweet)
        TextView tvSweet;

        @BindColor(R.color.input_red)
        int colorRed;
        @BindColor(R.color.text_livecard_hot)
        int colorNormal;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
